home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’95 / Menu Controls / PatternMenu.cp < prev    next >
Text File  |  1995-06-24  |  14KB  |  451 lines

  1. // Copyright © 1994-95 by Apple Computer, Inc. All rights reserved.
  2. // PatternMenu.cp
  3.  
  4. /*
  5.     Change History:
  6.         02/22/95    srf        1222899 Use CopyPat when appropriate
  7.         02/22/95    srf        1222850 MacApp should use MABlockMove and MABlockMoveOverlap everywhere
  8. */
  9.  
  10. #ifndef __PATTERNMENU__
  11. #include "PatternMenu.h"
  12. #endif
  13.  
  14. // MacApp
  15.  
  16. #ifndef __UDIALOG__
  17. #include <UDialog.h>
  18. #endif
  19.  
  20. #ifndef __UWINDOW__
  21. #include <UWindow.h>
  22. #endif
  23.  
  24. #ifndef __UMACAPPUTILITIES__
  25. #include <UMacAppUtilities.h>
  26. #endif
  27.  
  28. // Toolbox
  29.  
  30. #ifndef __RESOURCES__
  31. #include <Resources.h>
  32. #endif
  33.  
  34. // DrawShapes
  35.  
  36. #ifndef __SHAPECOMMANDS__
  37. #include "ShapeCommands.h"
  38. #endif
  39.  
  40. #ifndef __UDRAWSHAPES__
  41. #include "UDrawShapes.h"
  42. #endif
  43.  
  44. //----------------------------------------------------------------------------------------
  45. // Globals
  46.  
  47. TPatternsMenu*    gPatternsMenu = NULL;
  48. TWindow*        gPatternsWindow = NULL;
  49.  
  50. Pattern            gPat[kTotalPatterns];            // array of Patterns
  51.  
  52. //----------------------------------------------------------------------------------------
  53. // InitPatternsMenu: 
  54. //----------------------------------------------------------------------------------------
  55. #pragma segment AInit
  56.  
  57. void InitPatternsMenu()
  58. {
  59.     // Copy patterns into global array of patterns
  60.     Handle patListHandle = GetResource('PAT#', 128);
  61.     if (patListHandle == NULL)
  62.     {
  63.         CopyPat(qd.white, gPat[0]);
  64.         CopyPat(qd.ltGray, gPat[1]);
  65.         CopyPat(qd.gray, gPat[2]);
  66.         CopyPat(qd.dkGray, gPat[3]);
  67.         CopyPat(qd.black, gPat[4]);
  68.         for (short i=5; i < kTotalPatterns; i++)
  69.             CopyPat(qd.black, gPat[i]);
  70.     }
  71.     else
  72.     {
  73.         short oldState = LockHandleHigh(patListHandle);
  74.         short* sizePtr = (short*)(*patListHandle);
  75.         long numPatterns = Min(kTotalPatterns, *sizePtr);    // first word in pattern list is array size
  76.         // Calculate address where patterns start
  77.         Ptr address = (Ptr)((long)(*patListHandle) + sizeof(short));
  78.         for (short i=0; i < numPatterns; i++)
  79.         {
  80.             MABlockMove(address, &gPat[i], sizeof(Pattern));
  81.             address += sizeof(Pattern);
  82.         }
  83.         HSetState(patListHandle, oldState);
  84.     }
  85. }
  86.  
  87. //========================================================================================
  88. // CLASS TPatternsMenu
  89. //========================================================================================
  90. #undef Inherited
  91. #define Inherited TTearOffMenuView
  92.  
  93. #pragma segment AInit
  94. MA_DEFINE_CLASS_M1(TPatternsMenu, Inherited);
  95.  
  96. //----------------------------------------------------------------------------------------
  97. // TPatternsMenu Constructor
  98. //----------------------------------------------------------------------------------------
  99. #pragma segment AInit
  100.  
  101. TPatternsMenu::TPatternsMenu()
  102. {
  103. }
  104.  
  105. //----------------------------------------------------------------------------------------
  106. // TPatternsMenu::IPatternsMenu
  107. //----------------------------------------------------------------------------------------
  108. #pragma segment AInit
  109.  
  110. void TPatternsMenu::IPatternsMenu(ResNumber menuID)
  111. {
  112.     this->ITearOffMenuView(menuID, kWPatternsChoice, kHPatternsChoice, gPatternsWindow);
  113.  
  114.     TPatternsPalette* aPatternsPalette = new TPatternsPalette;
  115.     aPatternsPalette->IPatternsPalette(this);
  116.     aPatternsPalette->fIdentifier = 'MPLT';    // Identifies the patterns palette in the menu
  117. }
  118.  
  119. //========================================================================================
  120. // CLASS TPatternsPalette
  121. //========================================================================================
  122. #undef Inherited
  123. #define Inherited TView
  124.  
  125. #pragma segment AOpen
  126. MA_DEFINE_CLASS_M1(TPatternsPalette, Inherited);
  127.  
  128. //----------------------------------------------------------------------------------------
  129. // TPatternsPalette Constructor
  130. //----------------------------------------------------------------------------------------
  131. #pragma segment AOpen
  132.  
  133. TPatternsPalette::TPatternsPalette()
  134. {
  135.     fCurrPattern = 0;                // the pattern currently selected by user
  136.     fOldPattern = 0;                // the pattern previously selected by user
  137.     fSelectedPattern = 0;            // the pattern selected by user during tracking
  138.  
  139.     this->BuildChoiceArray();
  140.     this->BuildFramesArray();
  141. }
  142.  
  143. //----------------------------------------------------------------------------------------
  144. // TPatternsPalette::IPatternsPalette
  145. //----------------------------------------------------------------------------------------
  146. #pragma segment AOpen
  147.  
  148. void TPatternsPalette::IPatternsPalette(TView* itsSuperView)
  149. {
  150.     VPoint itsSize(kWPatternsChoice, kHPatternsChoice);
  151.     this->IView(NULL, itsSuperView, gZeroVPt, itsSize, sizeFixed, sizeFixed);
  152. }
  153.  
  154. //----------------------------------------------------------------------------------------
  155. // TPatternsPalette::DoPostCreate
  156. //----------------------------------------------------------------------------------------
  157. #pragma segment AOpen
  158.  
  159. void TPatternsPalette::DoPostCreate(TDocument* itsDocument)    // Override
  160. {
  161.     Inherited::DoPostCreate(itsDocument);
  162.  
  163.     this->AddAdorner(gSelectionAdorner, kDrawView, false);    // wants DoHighlightSelection
  164. }
  165.  
  166. //----------------------------------------------------------------------------------------
  167. // TPatternsPalette::BuildChoiceArray
  168. //----------------------------------------------------------------------------------------
  169. #pragma segment AOpen
  170.  
  171. void TPatternsPalette::BuildChoiceArray()
  172. {
  173.     // Create the Pattern choice rectangles that hold the Patterns
  174.     CRect r(0, 0, kWPatternsCell, kHPatternsCell);
  175.     short choiceID = 0;
  176.     for (short i=0; i<kPatternsAcross; i++)        // all the rectangles across
  177.     {
  178.         for (short j=0; j<kPatternsDown; j++)    // all the rectangles down
  179.         {
  180.             fChoiceArray[choiceID] = r;
  181.             OffsetRect(r, 0, kHPatternsCell + kFrameWidth);
  182.             choiceID++;
  183.         }
  184.         // Move rectangle over one column
  185.         SetRect(r, r.right + kFrameWidth, 0, r.right + kFrameWidth + kWPatternsCell, kHPatternsCell);
  186.     }
  187. }
  188.  
  189. //----------------------------------------------------------------------------------------
  190. // TPatternsPalette::BuildFramesArray
  191. //----------------------------------------------------------------------------------------
  192. #pragma segment AInit
  193.  
  194. void TPatternsPalette::BuildFramesArray()
  195. {
  196.     // Build the vertical borders between the patterns
  197.     CRect tempRect(kWPatternsCell, 0, kWPatternsCell + kFrameWidth, kHPatternsChoice);    /* left, top, right, bottom */
  198.     for (short i=0; i < kVerticalFrames; i++)
  199.     {
  200.         fFramesArray[i] = tempRect;
  201.         OffsetRect(tempRect, kWPatternsCell + kFrameWidth, 0);
  202.     }
  203.  
  204.     // build the horizontal borders between the patterns
  205.     SetRect(tempRect, 0, kHPatternsCell, kWPatternsChoice, kHPatternsCell + kFrameWidth);
  206.     for (short j=0; j < kHorizontalFrames; j++)
  207.     {
  208.         fFramesArray[j + kVerticalFrames] = tempRect;
  209.         OffsetRect(tempRect, 0, kHPatternsCell + kFrameWidth);
  210.     }
  211. }
  212.  
  213. //----------------------------------------------------------------------------------------
  214. // TPatternsPalette::DoHighlightSelection
  215. //----------------------------------------------------------------------------------------
  216. #pragma segment ShapeRes
  217.  
  218. void TPatternsPalette::DoHighlightSelection(HLState /*fromHL*/, HLState toHL) // Override
  219. {
  220.     if ((fCurrPattern >= 0) && (fCurrPattern < kTotalPatterns))
  221.     {
  222.         if (toHL == hlOff)
  223.         {
  224.             VRect area;
  225.             CRect r = fChoiceArray[fCurrPattern];
  226.             InsetRect(r, -5, -5);
  227.             this->QDToViewRect(r, area);
  228.             this->Draw(area);
  229.         }
  230.         else if (toHL == hlOn)
  231.             this->FrameCurrPattern();
  232.     }
  233. }
  234.  
  235. //----------------------------------------------------------------------------------------
  236. // TPatternsPalette::DoSetCursor
  237. //----------------------------------------------------------------------------------------
  238. #pragma segment ARes
  239.  
  240. void TPatternsPalette::DoSetCursor(const VPoint& /*localPoint*/, RgnHandle cursorRegion) // Override
  241. {
  242.     CRect qdExtent;
  243.  
  244.     SetCursor(&(qd.arrow));
  245.     this->GetQDExtent(qdExtent);
  246.     RectRgn(cursorRegion, qdExtent);
  247. }
  248.  
  249. //----------------------------------------------------------------------------------------
  250. // TPatternsPalette::Draw
  251. //----------------------------------------------------------------------------------------
  252. #pragma segment ARes
  253.  
  254. void TPatternsPalette::Draw(const VRect& area) // Override
  255. {
  256.     CRect qdArea;
  257.     CRect r;
  258.     CRect tempRect;
  259.     CRect drawRect;
  260.  
  261. #if qDebug
  262.     this->AssumeFocused();
  263. #endif
  264.     this->ViewToQDRect(area, qdArea);
  265.  
  266.     // draw the frames
  267.     for (short i=0; i < kTotalFrames; i++)
  268.     {
  269.         r = fFramesArray[i];
  270.         if (SectRect(r, qdArea, drawRect))
  271.             FrameRect(drawRect);
  272.     }
  273.  
  274.     // draw the Patterns
  275.     for (short j=0; j < kTotalPatterns; j++)
  276.     {
  277.         r = fChoiceArray[j];
  278.         if (SectRect(r, qdArea, drawRect))
  279.             FillRect(drawRect, &gPat[j]);
  280.     }
  281.  
  282.     Inherited::Draw(area);
  283. }
  284.  
  285. //----------------------------------------------------------------------------------------
  286. // TPatternsPalette::FrameCurrPattern
  287. //----------------------------------------------------------------------------------------
  288. #pragma segment ShapeRes
  289.  
  290. void TPatternsPalette::FrameCurrPattern()
  291. {
  292.     if (fCurrPattern != kNoPatternSelection)    // frame selected pattern, if any
  293.     {
  294.         CRect r = fChoiceArray[fCurrPattern];
  295.  
  296.         PenMode(patCopy);
  297.         InsetRect(r, - 1, - 1);
  298.         FrameRect(r);                            // frame it
  299.         InsetRect(r, - 3, - 3);                 // outer white frame
  300.         PenSize(3, 3);
  301.         PenPat(&qd.white);
  302.         FrameRect(r);
  303.         InsetRect(r, - 1, - 1);                 // outer black frame
  304.         PenSize(1, 1);
  305.         PenPat(&qd.black);
  306.         FrameRect(r);
  307.     }
  308. }
  309.  
  310. //----------------------------------------------------------------------------------------
  311. // TPatternsPalette::SelectNewPattern
  312. //----------------------------------------------------------------------------------------
  313. #pragma segment ShapeRes
  314.  
  315. void TPatternsPalette::SelectNewPattern(short whichPattern)
  316. {
  317.     if (this->IsShown())
  318.     {
  319.         this->Focus();
  320.         this->DoHighlightSelection(hlOn, hlOff);
  321.         fCurrPattern = whichPattern;
  322.         this->DoHighlightSelection(hlOff, hlOn);
  323.     }
  324.     else
  325.         fCurrPattern = whichPattern;    // so the correct pattern is selected when it is shown
  326. }
  327.  
  328. //----------------------------------------------------------------------------------------
  329. // TPatternsPalette::DoMouseCommand
  330. //----------------------------------------------------------------------------------------
  331. #pragma segment ASelCommand
  332.  
  333. void TPatternsPalette::DoMouseCommand(VPoint& theMouse,
  334.                                       TToolboxEvent* /*event*/,
  335.                                       CPoint /*hysteresis*/) // Override
  336. {
  337.     // find the active TShapeView
  338.     TShapeView* aShapeView = NULL;
  339.     TWindow* aWindow = gApplication->GetActiveWindow(kNoFloaters);
  340.     if (aWindow)
  341.         aShapeView = (TShapeView*)aWindow->FindSubView('SHAP');
  342.  
  343.     TPatternsPalette* menuPatternsView = NULL;
  344.     if (gPatternsMenu)
  345.         menuPatternsView = (TPatternsPalette*)gPatternsMenu->FindSubView('MPLT');
  346.  
  347.     TPatternsPalette* floatingPatternsView = NULL;
  348.     if (gPatternsWindow)
  349.         floatingPatternsView = (TPatternsPalette*)gPatternsWindow->FindSubView('PPLT');
  350.  
  351.     TReshadeCmd* aReshadeCmd = new TReshadeCmd;
  352.     aReshadeCmd->IReshadeCmd(cPatterns, aShapeView, this, menuPatternsView, floatingPatternsView, theMouse);
  353.  
  354.     this->PostCommand(aReshadeCmd);
  355. }
  356.  
  357. //----------------------------------------------------------------------------------------
  358. // TPatternsPalette::TrackFeedback
  359. //----------------------------------------------------------------------------------------
  360. #pragma segment ShapeRes
  361.  
  362. void TPatternsPalette::TrackFeedback(TrackPhase aTrackPhase,
  363.                                      const VPoint& /*anchorPoint*/,
  364.                                      const VPoint& /*previousPoint*/,
  365.                                      const VPoint& nextPoint,
  366.                                      Boolean /*mouseDidMove*/,
  367.                                      Boolean turnItOn)    // Override
  368. {
  369.     CPoint qdPt;
  370.     CRect  r;
  371.  
  372.     if (aTrackPhase == trackBegin)
  373.         fSelectedPattern = kNoPatternSelection;
  374.  
  375.     this->Focus();
  376.     qdPt = this->ViewToQDPt(nextPoint);
  377.     for (short i=0; i < kTotalPatterns; i++)
  378.     {
  379.         r = fChoiceArray[i];
  380.         if (fSelectedPattern == kNoPatternSelection)
  381.             InsetRect(r, -1, -1);            // Inset the rect by the frame
  382.         if (PtInRect(qdPt, r) && (fSelectedPattern != i))
  383.         {
  384.             if (fSelectedPattern == kNoPatternSelection)
  385.                 InsetRect(r, 1, 1);                // Set it back for drawing
  386.             this->TurnOnPattern(r, i);
  387.             if (turnItOn)
  388.             {
  389.                 if (fSelectedPattern != kNoPatternSelection)
  390.                 {
  391.                     r = fChoiceArray[fSelectedPattern];
  392.                     this->TurnOffPattern(r, fSelectedPattern);
  393.                 }
  394.             fSelectedPattern = i;
  395.             }
  396.             else
  397.                 fSelectedPattern = kNoPatternSelection;
  398.             break;
  399.         }
  400.     }
  401.  
  402.     if (aTrackPhase == trackEnd)
  403.     {
  404.         if (fSelectedPattern != kNoPatternSelection)
  405.         {
  406.             r = fChoiceArray[fSelectedPattern];
  407.             this->TurnOffPattern(r, fSelectedPattern);
  408.         }
  409.     }
  410. }
  411.  
  412. //----------------------------------------------------------------------------------------
  413. // TPatternsPalette::TurnOffPattern
  414. //----------------------------------------------------------------------------------------
  415. #pragma segment ShapeRes
  416.  
  417. void TPatternsPalette::TurnOffPattern(CRect patternRect, short whichPattern)
  418. {
  419.     PenNormal();
  420.     FillRect(patternRect, &gPat[whichPattern]);    // draw pattern
  421.     InsetRect(patternRect, - 1, - 1);            // draw frame
  422.     FrameRect(patternRect);
  423. //    this->FrameCurrPattern();                    // frame for current pattern
  424. }
  425.  
  426. //----------------------------------------------------------------------------------------
  427. // TPatternsPalette::TurnOnPattern
  428. //----------------------------------------------------------------------------------------
  429. #pragma segment ShapeRes
  430.  
  431. void TPatternsPalette::TurnOnPattern(CRect patternRect, short /*whichPattern*/)
  432. {
  433.     PenNormal();
  434.     PenMode(patXor);
  435.     InsetRect(patternRect, - 1, - 1);
  436.     PenSize(2, 2);
  437.     FrameRect(patternRect);
  438. }
  439.  
  440. //----------------------------------------------------------------------------------------
  441. // RandomPattern
  442. //----------------------------------------------------------------------------------------
  443. #pragma segment ADoCommand
  444.  
  445. short RandomPattern()
  446. {
  447.     return 1 + abs(Random() % kTotalPatterns);
  448. }
  449.  
  450.  
  451.